package JFrame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import static Tools.Tool.*;

public class GameJPanel extends JPanel implements Runnable {
    public ArrayList<Player> players=new ArrayList<>();
    public int Player_count;
    public int count;
    public int s;
    BufferedImage floors;
    private Set<Integer> PressedKeys = new HashSet<>();
    private Set<Integer> ReleasedKeys = new HashSet<>();
    public GameJPanel(int i){
        this.setBackground(Color.white);
        try {
            // 读取地板图片
            floors = ImageIO.read(new FileInputStream(imgpath1));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 计算绘制地板所需数量
        count=1080/floors.getWidth();
        s=1080/floors.getHeight();
        setFocusable(true);
        // 创建指定数量的玩家
        for(Player_count=0;Player_count<i;Player_count++){
            Random random=new Random();
            // 随机生成玩家的初始位置
            int x=random.nextInt(1080-26);
            int y=random.nextInt(200)+s*floors.getHeight()-256;
            players.add(new Player(x,y,"P"+Player_count));
        }
        // 监听键盘事件
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                add_KEY_EVENT(e);
            }
        });
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                released_KEY_EVENT(e);
            }
        });
        // 启动线程
        new Thread(this).start();
    }

    public void run(){
        while(true){
            try {
                judge(); // 判断游戏是否结束
                check_KEY_EVENT(); // 处理键盘事件
                this.repaint(); // 重新绘制界面
                Thread.sleep(20);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    // 判断游戏胜负
    public void judge(){
        for(Player P:players){
            if(P.life<=0){
                JOptionPane.showMessageDialog(null, "胜负已出!"+P.name+"是菜狗?");
                System.exit(0);
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        int x=0;
        // 绘制地板
        for(int i=0;i<=count+1;i++) {
            g.drawImage(floors, x, s*floors.getHeight()-80, null);
            x+=floors.getWidth();
        }
        // 绘制玩家
        for(int i=0;i<=Player_count-1;i++){
            players.get(i).draw(g);
        }
        judgerects(); // 判断玩家之间的碰撞
    }

    // 处理按键按下事件
    public void add_KEY_EVENT(KeyEvent e){
        PressedKeys.add(e.getKeyCode()); // 将按下的键添加到集合中
    }

    // 处理按键释放事件
    public void released_KEY_EVENT(KeyEvent e){
        PressedKeys.remove(e.getKeyCode());
        ReleasedKeys.add(e.getKeyCode());
    }

    // 检查按键事件
    public void check_KEY_EVENT(){
        long currentTime_kill = System.currentTimeMillis();
        for(Integer Press:PressedKeys){
            switch (Press){
                // 玩家1控制
                case KeyEvent.VK_A:
                    players.get(0).walk=true;
                    players.get(0).direction=false;
                    players.get(0).Player_v_level-=2;
                    break;
                case KeyEvent.VK_D:
                    players.get(0).walk=true;
                    players.get(0).direction=true;
                    players.get(0).Player_v_level+=2;
                    break;
                case KeyEvent.VK_U:
                    // 发射子弹
                    if (currentTime_kill - players.get(0).lastKPressTime_bullet > 1000) {
                        players.get(0).lastKPressTime_bullet = currentTime_kill;
                        players.get(0).setKills(players.get(0).Player_x, players.get(0).Player_y, players.get(0).direction, 0);
                    }
                    break;
                case KeyEvent.VK_I:
                    // 发射火球
                    if (currentTime_kill - players.get(0).lastKPressTime_fire> 1000) {
                        players.get(0).lastKPressTime_fire = currentTime_kill;
                        players.get(0).setKills(players.get(0).Player_x, players.get(0).Player_y + 18, players.get(0).direction, 1);
                    }
                    break;
                case KeyEvent.VK_O:
                    // 发射镇压技能
                    if (currentTime_kill - players.get(0).lastKPressTime_zhen> 4500) {
                        players.get(0).lastKPressTime_zhen = currentTime_kill;
                        players.get(0).setKills(players.get(0).Player_x,players.get(0).Player_y-188,players.get(0).direction, 2);
                    }
                    for(Kill K:players.get(0).kills){
                        if(K.type==2&&!K.release){
                            K.addx();
                        }
                    }
                    break;
                // 玩家2控制
                case KeyEvent.VK_LEFT:
                    players.get(1).walk=true;
                    players.get(1).direction=false;
                    players.get(1).Player_v_level-=2;
                    break;
                case KeyEvent.VK_RIGHT:
                    players.get(1).walk=true;
                    players.get(1).direction=true;
                    players.get(1).Player_v_level+=2;
                    break;
                case KeyEvent.VK_NUMPAD4:
                    // 发射子弹
                    if (currentTime_kill - players.get(1).lastKPressTime_bullet > 1000) {
                        players.get(1).lastKPressTime_bullet = currentTime_kill;
                        players.get(1).setKills(players.get(1).Player_x, players.get(1).Player_y, players.get(1).direction, 0);
                    }
                    break;
                case KeyEvent.VK_NUMPAD5:
                    // 发射火球
                    if (currentTime_kill - players.get(1).lastKPressTime_fire > 1000) {
                        players.get(1).lastKPressTime_fire = currentTime_kill;
                        players.get(1).setKills(players.get(1).Player_x, players.get(1).Player_y + 18, players.get(1).direction, 1);
                    }
                    break;
                case KeyEvent.VK_NUMPAD6:
                    // 发射

                    //镇压技能
                    if (currentTime_kill - players.get(1).lastKPressTime_zhen> 4500) {
                        players.get(1).lastKPressTime_zhen = currentTime_kill;
                        players.get(1).setKills(players.get(1).Player_x,players.get(1).Player_y-188,players.get(1).direction, 2);
                    }
                    for(Kill K:players.get(1).kills){
                        if(K.type==2){
                            K.addx();
                        }
                    }
                    break;
            }
        }
        // 处理释放按键事件
        for(Integer Released:ReleasedKeys){
            switch (Released){
                // 玩家1控制
                case KeyEvent.VK_K:
                    // 跳跃
                    if (currentTime_kill - players.get(0).lastKPressTime_jump > 400) {
                        players.get(0).lastKPressTime_jump = currentTime_kill;
                        players.get(0).Player_v_vertical += 25;
                    }
                    break;
                case KeyEvent.VK_A:
                    players.get(0).walk=false;
                    players.get(0).image_counts=19;
                    break;
                case KeyEvent.VK_D:
                    players.get(0).walk=false;
                    players.get(0).image_counts=5;
                    break;
                case KeyEvent.VK_O:
                    // 释放镇压技能
                    for(Kill K:players.get(0).kills){
                        if(K.type==2){
                            K.release=true;
                            K.setlife_time();
                        }
                    }
                    break;
                // 玩家2控制
                case KeyEvent.VK_NUMPAD2:
                    // 跳跃
                    if (currentTime_kill - players.get(1).lastKPressTime_jump > 400) {
                        players.get(1).lastKPressTime_jump = currentTime_kill;
                        players.get(1).Player_v_vertical += 25;
                    }
                    break;
                case KeyEvent.VK_LEFT:
                    players.get(1).walk=false;
                    players.get(1).image_counts=19;
                    break;
                case KeyEvent.VK_RIGHT:
                    players.get(1).walk=false;
                    players.get(1).image_counts=5;
                    break;
                case KeyEvent.VK_NUMPAD6:
                    // 释放镇压技能
                    for(Kill K:players.get(1).kills){
                        if(K.type==2){
                            K.release=true;
                            K.setlife_time();
                        }
                    }
                    break;
            }
        }
        ReleasedKeys.clear();
    }

    // 判断玩家之间的碰撞
    public void judgerects() {
        long currentTime_life = System.currentTimeMillis();
        if (Player_count == 2) {
            // 玩家1和玩家2碰撞检测
            for (Rectangle R : players.get(0).rects) {
                // 判断玩家2是否处于无敌状态
                if (currentTime_life - players.get(1).lastKPressTime_invincibility > 1500) {
                    players.get(1).invincibility = false;
                } else {
                    players.get(1).invincibility = true;
                }
                // 玩家1和玩家2碰撞检测
                if (R.intersects(players.get(1).rect_player)) {
                    if (!players.get(1).invincibility) {
                        if (R.getWidth() == 82) {
                            players.get(1).life -= 2;
                            players.get(1).invincibility = true;
                            players.get(1).lastKPressTime_invincibility = currentTime_life;
                        } else if (R.getWidth() == 45) {
                            players.get(1).life -= 1;
                            players.get(1).invincibility = true;
                            players.get(1).lastKPressTime_invincibility = currentTime_life;
                        } else {
                            // 判断玩家1是否在释放镇压技能
                            for (Kill K : players.get(0).kills) {
                                if (currentTime_life - K.life_time > 1050 && currentTime_life - K.life_time < 1300) {
                                    players.get(1).life -= 5;
                                    players.get(1).invincibility = true;
                                    players.get(1).lastKPressTime_invincibility = currentTime_life;
                                }
                            }
                        }

                    }
                }
            }
            // 玩家2和玩家1碰撞检测
            for (Rectangle R : players.get(1).rects) {
                // 判断玩家1是否处于无敌状态
                if (currentTime_life - players.get(0).lastKPressTime_invincibility > 1500) {
                    players.get(0).invincibility = false;
                } else {
                    players.get(0).invincibility = true;
                }
                // 玩家2和玩家1碰撞检测
                if (R.intersects(players.get(0).rect_player)) {
                    if (!players.get(0).invincibility) {
                        if (R.getWidth() == 82) {
                            players.get(0).life -= 2;
                            players.get(0).invincibility = true;
                            players.get(0).lastKPressTime_invincibility = currentTime_life;
                        } else if (R.getWidth() == 45) {
                            players.get(0).life -= 1;
                            players.get(0).invincibility = true;
                            players.get(0).lastKPressTime_invincibility = currentTime_life;
                        } else {
                            // 判断玩家2是否在释放镇压技能
                            for (Kill K : players.get(1).kills) {
                                if (currentTime_life - K.life_time > 1050 && currentTime_life - K.life_time <1300) {
                                    players.get(0).life -= 5;
                                    players.get(0).invincibility = true;
                                    players.get(0).lastKPressTime_invincibility = currentTime_life;
                                }
                            }
                        }

                    }
                }
            }
            players.get(0).rects.clear();
            players.get(1).rects.clear();
        }
    }
}
